home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac: Not for Sale / Another.not.for.sale (Australia).iso / fade into you / getting there / TCP⁄PPP⁄SLIP / TCP⁄IP Scripting Addition 1.1.2 / Examples 1.2 / Text Versions / Drag and Drop FTP 1.1 (text) < prev    next >
Text File  |  1994-09-18  |  6KB  |  183 lines

  1. (*
  2. Drag and Drop FTP
  3. version 1.1
  4. Copyright © 1994 by Atul Butte. All Rights Reserved.
  5.  
  6. Run me without any documents to first set destination host and user-id. Then drop
  7. files on me and they will be sent to that host via FTP.
  8.  
  9. Atul Butte
  10. atul_butte@brown.edu
  11. *)
  12.  
  13. global LF, CR, CRLF
  14.  
  15. property ftp_set : false
  16. property ftp_host : ""
  17. property ftp_user : ""
  18. property ftp_directory : ""
  19. property ftp_transfer_type : ""
  20.  
  21. on run
  22.     set dialog_response to (display dialog "Enter the name of the machine where you want to send files dropped on me" default answer ftp_host)
  23.     if (button returned of dialog_response ≠ "OK") then
  24.         return
  25.     else
  26.         set ftp_host to text returned of dialog_response
  27.     end if
  28.     
  29.     set dialog_response to (display dialog "Enter your user–id on " & ftp_host default answer ftp_user)
  30.     if (button returned of dialog_response ≠ "OK") then
  31.         return
  32.     else
  33.         set ftp_user to text returned of dialog_response
  34.     end if
  35.     
  36.     set dialog_response to (display dialog "Enter the directory on " & ftp_host & " in which to store the files, or press Return for default directory" default answer ftp_directory)
  37.     if (button returned of dialog_response ≠ "OK") then
  38.         return
  39.     else
  40.         set ftp_directory to text returned of dialog_response
  41.     end if
  42.     
  43.     set dialog_response to (display dialog "Do you want these downloaded as (1) MacBinary files, (2) text files, or (3) raw data files?" default answer "1")
  44.     if (button returned of dialog_response ≠ "OK") then
  45.         return
  46.     else
  47.         set ftp_transfer_type to ((text returned of dialog_response) as integer)
  48.     end if
  49.     
  50.     set ftp_set to true
  51. end run
  52.  
  53. on open (docList)
  54.     set LF to (ASCII character of 10)
  55.     set CR to (ASCII character of 13)
  56.     set CRLF to CR & LF
  57.     set oldDelimiters to AppleScript's text item delimiters
  58.     set AppleScript's text item delimiters to {"."}
  59.     set localAddr to (tcp my address)
  60.     set localAddrList to text items of localAddr
  61.     set AppleScript's text item delimiters to {","}
  62.     set localAddrCommaString to localAddrList as string
  63.     set AppleScript's text item delimiters to oldDelimiters
  64.     
  65.     if not ftp_set then
  66.         run
  67.     end if
  68.     
  69.     set dialog_response to (display dialog "Enter the password for " & ftp_user & "@" & ftp_host & return & "Note: what you type is visible!" default answer "")
  70.     if (button returned of dialog_response ≠ "OK") then
  71.         return
  72.     else
  73.         set ftp_password to text returned of dialog_response
  74.     end if
  75.     
  76.     set sss to (tcp connect to host ftp_host port 21)
  77.     try
  78.         readresponse(sss)
  79.         if (ftp_user ≠ "") then
  80.             tcp write stream sss data "USER " & ftp_user & return using ISO88591
  81.             readresponse(sss)
  82.         end if
  83.         if (ftp_password ≠ "") then
  84.             tcp write stream sss data "PASS " & ftp_password & return using ISO88591
  85.             readresponse(sss)
  86.         end if
  87.         if (ftp_directory ≠ "") then
  88.             tcp write stream sss data "CWD " & ftp_directory & return using ISO88591
  89.             readresponse(sss)
  90.         end if
  91.         
  92.         repeat with aFile in docList
  93.             set aFile to contents of aFile
  94.             
  95.             if ftp_transfer_type ≠ 2 then
  96.                 tcp write stream sss data "TYPE I" & return using ISO88591
  97.                 readresponse(sss)
  98.             else
  99.                 tcp write stream sss data "TYPE A" & return using ISO88591
  100.                 readresponse(sss)
  101.             end if
  102.             
  103.             set data_stream to (tcp wait for connect)
  104.             set data_stream_status to (tcp status stream data_stream)
  105.             set portHiByte to round (local port of data_stream_status) / 256 rounding down
  106.             set portLoByte to (local port of data_stream_status) mod 256
  107.             tcp write stream sss data "PORT " & localAddrCommaString & "," & portHiByte & "," & portLoByte & return using ISO88591
  108.             readresponse(sss)
  109.             
  110.             set afilename to aFile as string
  111.             set oldDelimiters to AppleScript's text item delimiters
  112.             set AppleScript's text item delimiters to {":"}
  113.             set theFileName to last text item of afilename
  114.             set AppleScript's text item delimiters to {" "}
  115.             set d to text items of theFileName
  116.             set AppleScript's text item delimiters to {"_"}
  117.             set theFileName to d as string
  118.             set AppleScript's text item delimiters to oldDelimiters
  119.             
  120.             tcp write stream sss data "STOR " & theFileName & return & LF
  121.             readresponse(sss)
  122.             
  123.             repeat until (connection status of (tcp status stream data_stream) = Connected)
  124.             end repeat
  125.             try
  126.                 if ftp_transfer_type = 1 then
  127.                     tcp send stream data_stream source (aFile) using MacBinary
  128.                 else if ftp_transfer_type = 2 then
  129.                     tcp send stream data_stream source (aFile) using ISO88591
  130.                 else if ftp_transfer_type = 3 then
  131.                     tcp send stream data_stream source (aFile) using Raw Data Fork
  132.                 end if
  133.             on error msg number num from obj partial result pr
  134.                 tcp close stream data_stream
  135.                 error msg number num from data_stream
  136.             end try
  137.             tcp close stream data_stream
  138.             
  139.             readresponse(sss)
  140.         end repeat
  141.         
  142.         tcp write stream sss data "QUIT" & return using ISO88591
  143.         readresponse(sss)
  144.         
  145.         tcp close stream sss
  146.         return
  147.     on error msg number num from obj partial result pr
  148.         try
  149.             tcp write stream sss data "QUIT" & return using ISO88591
  150.             readresponse(sss)
  151.         on error
  152.         end try
  153.         
  154.         tcp close stream sss
  155.         error msg number num from obj partial result pr
  156.     end try
  157. end open
  158.  
  159. on readresponse(sstream)
  160.     set continuechar to "-"
  161.     set wholemessage to ""
  162.     repeat until continuechar = " "
  163.         repeat until (tcp ahead characters LF stream sstream)
  164.         end repeat
  165.         set readline to (tcp read stream sstream until characters LF using ISO88591)
  166.         set scan to (scanline(readline))
  167.         set continuechar to item 2 of scan
  168.         set wholemessage to wholemessage & " " & item 3 of scan
  169.     end repeat
  170.     set errorCode to item 1 of scan as integer
  171.     if (errorCode ≥ 400) then
  172.         display dialog "Error: " & wholemessage
  173.         error wholemessage number errorCode
  174.     end if
  175. end readresponse
  176.  
  177. -- Matches lines like:
  178. -- 250 test... Sender ok
  179.  
  180. on scanline(lline)
  181.     return {characters 1 through 3 of lline as string, character 4 of lline as string, ¬
  182.         characters 5 through end of lline as string}
  183. end scanline